package com.yazi.mxz.person.controller.shop;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.myframework.smhj.jackson.result.ObjectResult;
import com.yazi.mxz.common.entity.beans.Member;
import com.yazi.mxz.common.entity.beans.MemberVip;
import com.yazi.mxz.common.entity.beans.MemberVipId;
import com.yazi.mxz.common.service.MemberService;
import com.yazi.mxz.common.service.MemberVipService;
import com.yazi.mxz.person.jackson.filter.MemberVipFilter;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller("shop-memberVipController")
@RequestMapping("/shop/memberVip")
public class MemberVipController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(MemberVipController.class);
	@Resource
	MemberVipService memberVipService;
	@Resource
	MemberService memberService;
	
	@RequestMapping
	public String details() {
		debug(log, "进入企业会员信息页面");
		return "shop/memberVip_details";
	}
	
	@RequestMapping("/searchDo/json")
	@ResponseBody
	public ObjectResult<?> searchDoJson(String name) {
		debug(log, "按账户名查询企业会员");
		ObjectResult<MemberVip> jsonResult = new ObjectResult<MemberVip>();
		jsonResult.setFilter(MemberVipFilter.getWithMember());
		Member member;
		MemberVip memberVip;
		try {
			member = memberService.oneByAccountName(name);
			if (null == member) {
				jsonResult.setFailMessage("会员不存在");
				return jsonResult;
			}
			MemberVipId id = new MemberVipId();
			id.setCompanyId(getCompanyId());
			id.setMemberId(member.getMemberId());
			memberVip = memberVipService.one(id);
			if (null == memberVip) {
				jsonResult.setFailMessage("会员不存在");
				return jsonResult;
			}
			jsonResult.setResult(memberVip);
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "用户查询异常");
			jsonResult.setErrorMessage("操作失败，请联系管理员");
		}
		
		return jsonResult;
	}
	

	@RequestMapping("/balance/json")
	@ResponseBody
	public Map<String, Object> balanceJson(String userName) {
		debug(log, "检查用户余额");
		Map<String, Object> map = new HashMap<String, Object>();
		Member member = null;
		try {
			member = memberService.oneByAccountName(userName);
			if (null != member) {
				MemberVipId mvId = new MemberVipId();
				mvId.setMemberId(member.getMemberId());
				mvId.setCompanyId(getCompanyId());
				MemberVip mv = memberVipService.one(mvId);
				if (null == mv) {
					mv = new MemberVip();
					mv.setBalance(0f);
					mv.setPrepaidMax(0f);
					mv.setPrepaidTotal(0f);
				}
				
				map.put("ok", "");
				map.put("balance", mv.getBalance());
				map.put("prepaidMax", mv.getPrepaidMax());
				map.put("prepaidTotal", mv.getPrepaidTotal());
				map.put("canPrepaid", getCompany(true).getPrepaid() - mv.getBalance());
			} else {
				map.put("error", "用户不存在");
			}
		} catch (Exception e) {
			error(log, e, "检查用户异常");
			map.put("error", e.getMessage());
		}
	
		return map;
	}
}
